package com.example.contractcontentprovidertest;

import static com.example.contractcontentprovidertest.R.color.cardview_shadow_end_color;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContractRecyclerAdapter extends RecyclerView.Adapter {
    //展示数据
    List<Map> contracts;
    //设计一个内部类进行holder展示
    class MyContractRecyclerHolder extends  RecyclerView.ViewHolder{
        TextView textName;
        ListView listViewPhone;
        public MyContractRecyclerHolder(@NonNull View itemView) {
            super(itemView);
            textName = itemView.findViewById(R.id.text_name);
            listViewPhone = itemView.findViewById(R.id.list_view_phone);
        }
    } 
    //构造函数
    public ContractRecyclerAdapter(List<Map> contracts) {
        this.contracts = contracts;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //先视图后逻辑
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contract_item,parent,false);
        MyContractRecyclerHolder holder = new MyContractRecyclerHolder(view);
        HashMap man = (HashMap) contracts.get(viewType);
        String name = (String) man.get("name");
        ArrayList<String> phones = (ArrayList<String>) man.get("phones");
        MyContractRecyclerHolder contractRecyclerHolder = (MyContractRecyclerHolder) holder;
        contractRecyclerHolder.textName.setText(name);
        contractRecyclerHolder.textName.setBackgroundColor(cardview_shadow_end_color);
        ArrayAdapter<String> phonesAdapter = new ArrayAdapter<>(parent.getContext(),R.layout.support_simple_spinner_dropdown_item,phones);
        contractRecyclerHolder.listViewPhone.setAdapter(phonesAdapter);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    }
    //返回下标
    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return contracts.size();
    }
}
